import { isEqual } from '@/common/plugins/tools/type'

export default {
  props: {},
  data() {
    return {
      warrantFeeRecordTableName: 'credit_warrant_fee_record',
      warrantFeeRecords: [],
      warrantFeeRecordExists: [],
    }
  },
  computed: {
    warrantFeeRecordTotal() {
      const total = this.warrantFeeRecords.reduce(
        (prev, curr) => prev + (Number.parseFloat(curr.due_warrant_fee) || 0),
        0
      )
      return Math.round(total * 100) / 100
    },
    warrantFeeRecordCount() {
      return this.warrantFeeRecords.length
    },
  },
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async getWarrantFeeRecord(relatedId) {
      if (!relatedId) return

      const [res] = await this.$utils.promise(
        this.$api.getData(
          this.$serviceName,
          this.warrantFeeRecordTableName,
          {
            'customer_related_id.eq': relatedId,
          },
          { due_warrant_date: 'asc' }
        )
      )

      this.warrantFeeRecordExists = (res && res.data) || []
      this.warrantFeeRecords = this.warrantFeeRecordExists.map(item => ({ ...item }))
    },

    async saveWarrantFeeRecord(relatedId) {
      // 检查需要修改的项目
      const changeItems = this.checkWarrantFeeRecordChange(relatedId)

      if (!changeItems.length) return false

      return this.putWarrantFeeRecord(changeItems)
    },

    checkWarrantFeeRecordChange(relatedId) {
      const changeItems = []
      for (const editItem of this.warrantFeeRecords) {
        if (!editItem.id) {
          changeItems.push({
            customer_related_id: editItem.customer_related_id || relatedId,
            due_warrant_fee: editItem.due_warrant_fee,
            due_warrant_date: editItem.due_warrant_date,
          })
          continue
        }

        const existItem = this.warrantFeeRecordExists.find(exist => exist.id === editItem.id)
        if (!existItem) continue

        if (!isEqual(existItem, editItem)) {
          changeItems.push({
            id: existItem.id,
            due_warrant_fee: editItem.due_warrant_fee,
            due_warrant_date: editItem.due_warrant_date,
          })
        }
      }

      return changeItems
    },

    putWarrantFeeRecord(data) {
      return this.$api.updateBatch(this.$serviceName, this.warrantFeeRecordTableName, data)
    },

    deleteWarrantFeeRecordById(id, relatedId) {
      return this.$utils
        .msgbox({
          message: `确定删除?`,
          type: 'warning',
          beforeClose: () =>
            this.$api.deleteData(this.$serviceName, this.warrantFeeRecordTableName, id),
        })
        .then(() => {
          this.$message.success('删除成功！')
          this.getWarrantFeeRecord(relatedId)
        })
        .catch(() => this.$message.error(`删除失败，请重试！`))
    },

    async updateWarrantFeeRecordById(id, data, relatedId) {
      await this.$api
        .updateData(this.$serviceName, this.warrantFeeRecordTableName, id, data)
        .then(() => {
          this.getWarrantFeeRecord(relatedId)
        })
    },
  },
}
